// 定时器任务
export const timeoutTask = (task, time) => {
  const tmpTimeout = setTimeout(() => {
    task();
    clearTimeout(tmpTimeout);
  }, time);
};

// 获取url中的参数
export const getUrlParam = (key) => {
  const tmpRes = window.location.search.match(new RegExp(`[?&]${key}=([^&]+)`, 'i'));
  let result;
  if (tmpRes === null || tmpRes.length < 1) {
    result = '';
  } else {
    [, result] = tmpRes;
  }
  return result;
};

// 生成随机数(min到max,不包括max)
export const generateRandom = (min, max) => {
  return Math.floor(Math.random() * (max - min)) + min;
};

// 生成随机id(随机字母加当前时间戳构成)
export const generateRandomId = (pLen) => {
  let tmpId = '';
  const charStr = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz';
  const tmpLen1 = charStr.length;
  const tmplen2 = pLen || 6; // 生成的id字母部分的长度，默认长度为6
  for (let i = 0; i < tmplen2; i += 1) {
    tmpId += charStr.charAt(Math.floor(Math.random() * tmpLen1));
  }
  tmpId += new Date().getTime();
  return tmpId;
};
